//
//  BCDateTool.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/6/21.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCDateTool.h"

@implementation BCDateTool

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Dalian"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
}

+ (NSInteger)weekDay:(NSDate *)inputDate {
    NSInteger currentWeekDay = 0;
    NSString *currentWeekDayString = [BCDateTool weekdayStringFromDate:inputDate];
    if ([currentWeekDayString isEqualToString:@"星期一"]) {
        currentWeekDay = 1;
    }
    else if ([currentWeekDayString isEqualToString:@"星期二"]) {
        currentWeekDay = 2;
    }
    else if ([currentWeekDayString isEqualToString:@"星期三"]) {
        currentWeekDay = 3;
    }
    else if ([currentWeekDayString isEqualToString:@"星期四"]) {
        currentWeekDay = 4;
    }
    else if ([currentWeekDayString isEqualToString:@"星期五"]) {
        currentWeekDay = 5;
    }
    else if ([currentWeekDayString isEqualToString:@"星期六"]) {
        currentWeekDay = 6;
    }
    else {
        currentWeekDay = 7;
    }
    return currentWeekDay;
}

+ (NSString *)dateStringFromDate:(NSDate *)inputDate {
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"M月d日"];
    //用[NSDate date]可以获取系统当前时间
    return [dateFormatter stringFromDate:inputDate];
}

+ (NSDate *)GetDayWithIndex:(NSInteger)index From:(NSDate *)aDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Dalian"];
    [calendar setTimeZone: timeZone];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+index)];
    
    return [calendar dateFromComponents:components];
}

@end
